package com.symbio.ishenapi.laboratory.controller;

import com.symbio.ishenapi.common.bean.LoginUser;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.laboratory.model.TmGroupLaboratory;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:分组化验单管理控制层
 * @Date: Created in 11:31 2018/11/21
 * @Modified By:
 */
@RequestMapping("/TmGroupLaboratory")
public class TmGroupLaboratoryController extends BaseController{

    /*** POST
     * @Author: chen
     * @Description:分组里关联的化验单保存
     * @Date: 11:37 2018/11/21
     * @param: [
     *              guoupId        ---分组id
                    laboratoryId   ---化验单id
                    laboratoryName ---化验单名称
                    reviewMonth    ---复查时间 /月
     *         ]
     * @return: void
     */
    public void save(){
        try {
            TmGroupLaboratory laboratory = (TmGroupLaboratory)getReqBodyObject(TmGroupLaboratory.class);
            //获取登录信息
            LoginUser loginUser = getLoginUser();
            int isExist = TmGroupLaboratory.dao.isExist(laboratory.getLaboratoryId(),laboratory.getGroupId());
            if(isExist == 0){
                TmGroupLaboratory.dao.saveGroupLaboratory(laboratory,loginUser);
                renderJsonError(50001,"分组化验单保存");
            }else {
                renderJsonError(50003,"分组化验单已存在");
            }
        }catch (Exception e){
            renderJsonError(50002,"分组化验单保存");
        }
    }

    /** GET
     * @Author: chen
     * @Description:根据id查询分组化验里的化验单详情
     * @Date: 11:52 2018/11/21
     * @param: [
     *              id   ---化验单id
     *          ]
     * @return: void
     */
    public void findById(){
        try {
            String id = getPara("id");
            TmGroupLaboratory laboratory = TmGroupLaboratory.dao.findGroupLabById(id);
            renderJsonRet(laboratory);
        }catch (Exception e){
            renderJsonError(50002,"化验单修改回显查询");
        }
    }

    /** POST
     * @Author: chen
     * @Description:分组化验单修改
     * @Date: 12:00 2018/11/21
     * @param: [
     *              id   ---化验单id
     *              reviewMonth  ---复查期限
     *          ]
     * @return: void
     */
    public void update(){
        try {
            TmGroupLaboratory laboratory = (TmGroupLaboratory)getReqBodyObject(TmGroupLaboratory.class);
            //获取登录信息
            LoginUser loginUser = getLoginUser();
            TmGroupLaboratory.dao.updateGroupLabById(laboratory,loginUser);
            renderJsonError(50001,"分组化验单修改");
        }catch (Exception e){
            renderJsonError(50002,"分组化验单修改");
        }
    }

    /** GET
     * @Author: chen
     * @Description:分组化验单列表查询
     * @Date: 12:09 2018/11/21
     * @param: [
     *              guoupId   ---分组id
     *          ]
     * @return: void
     */
    public void findList(){
        try {
            String groupId = getPara("groupId");
            List<TmGroupLaboratory> data = TmGroupLaboratory.dao.findList(groupId);
            renderJsonRet(data);
        }catch (Exception e){
            renderJsonError(50002,"分组化验单列表查询");
        }
    }

    /** GET
     * @Author: chen
     * @Description:
     * @Date: 13:19 2018/11/21
     * @param: [
     *              ids   ---化验单id集合 ， 分隔
     *          ]
     * @return: void
     */
    public void delete(){
        try {
            String ids = getPara("ids");
            TmGroupLaboratory.dao.deleteGroupLabs(ids);
            renderJsonError(50001,"分组化验单删除");
        }catch (Exception e){
            renderJsonError(50002,"分组化验单删除");
        }
    }

    /** GET
     * @Author: chen
     * @Description:根据分组id获取组内化验单的ids集合
     * @Date: 13:29 2018/11/21
     * @param: [
     *              groupId    ---组id
     *          ]
     * @return: void
     */
    public void findLaboratoryIds(){
        try {
            String groupId = getPara("groupId");
            List<TmGroupLaboratory> laboratoryIds = TmGroupLaboratory.dao.findLaboratoryIds(groupId);
            renderJsonRet(laboratoryIds);
        }catch (Exception e){
            renderJsonError(50002,"分组化验单ids集合查询");
        }
    }

    //----------------------------------------------------------------------------------------------
    /** **
     * @Author: chen
     * @Description: 判断患者的化验单是否有效复查
     * @Date: 16:03 2018/11/21
     * @param: [
     *              userId   ---患者id
     *              groupId  ---分组id
     *          ]
     * @return: void
     */
    public void reviewLabByUserId(){
        try {
            String userId = getPara("userId");
            String groupId = getPara("groupId");
            Map<String,Object> data = TmGroupLaboratory.dao.reviewLabByUserId(userId,groupId);
            renderJsonRet(data);
        }catch (Exception e){
            renderJsonError(50002,"患者是否有效检查失败");
        }
    }
}
